const api = require("../../api/api")
const util = require("../../utils/util")
Page({
  data: {
    avatarUrl: undefined,
    avatarUrlTmpFile: undefined,
    gender: undefined,
    genderIndex: -1,
    nick: ''
  },
  onLoad: function (options) {
    //this.getUserApiInfo()
    let that = this;
    let home = wx.getStorageSync('home');
    api.user.home({
      success(res) {
        if(res.data.code == 0){
          that.setData({
            avatarUrl: res.data.data.head_image,
            nick: res.data.data.nickname,
          });
        }
   
      }
    })

  },
  onShow: function () {},
  // getPhoneNumber: function(e) {
  //   if (!e.detail.errMsg || e.detail.errMsg != "getPhoneNumber:ok") {
  //     wx.showModal({
  //       title: '提示',
  //       content: e.detail.errMsg,
  //       showCancel: false
  //     })
  //     return;
  //   }
  //   this._getPhoneNumber(e)
  // },
  // async getUserApiInfo() {
  //   const res = await WXAPI.userDetail(wx.getStorageSync('token'))
  //   if (res.code == 0) {
  //     let _data = {}
  //     _data.apiUserInfoMap = res.data
  //     _data.nick = res.data.base.nick
  //     _data.avatarUrl = res.data.base.avatarUrl
  //     if (!res.data.base.gender) {
  //       _data.gender = '未知'
  //     }
  //     if (res.data.base.gender == 1) {
  //       _data.gender = '男性'
  //     }
  //     if (res.data.base.gender == 2) {
  //       _data.gender = '女性'
  //     }
  //     this.setData(_data)
  //   }
  // },
  async formSubmit(e) {
    let that = this;
    const api = require("../../api/api")
    if (this.data.avatarUrlTmpFile) {
      api.user.updateHeadImage({
        head_image: that.data.avatarUrlTmpFile,
        success(res) {
          if (res.data.code == 0) {
            //取得个人中心信息
          }
        }
      })
    }
    if (this.data.nick) {
      api.user.updateNickname({
        nickname: that.data.nick,
        success(res) {
          if (res.data.code == 0) {

          }
        }
      })
    }
    api.user.home({
      success(res) {
        //更新home到缓存
        wx.setStorageSync('home', res.data.data);
      }
    })
    wx.showToast({
      title: '编辑成功',
    })
    setTimeout(() => {
      const util = require("../../utils/util")
      util.setTabbarActive('person');
      wx.redirectTo({
        url: '/pages/person/person',
      })
    }, 1000);
  },
  chooseImage() {
    //七牛库
    const qiniuUploader = require("../../utils/qiniuUploader");
    const _this = this
    wx.chooseImage({
      count: 1,
      success(res) {
        let filePath = res.tempFilePaths[0];
        const api = require("../../api/api");
        //取得上传token
        api.user.getUploadToken({
          success(res) {
            //token
            let token = res.data.data;
            //上传url
            // let uploadURL=util.getQiNiuUrl()+"/upload/"+dtStr;
            let uploadURL = util.getQiNiuUrl();

            //date
            let dt = new Date();
            let dtStr = "" + dt.getFullYear() + util.formatNumber(dt.getMonth() + 1) + util.formatNumber(dt.getDate())

            //filename
            let index = filePath.lastIndexOf("/")
            let filename = filePath.substr(index);
            //key
            let key = "upload/" + dtStr + filename;
            //调用七牛上传
            qiniuUploader.upload(filePath,
              function (res) {
                _this.setData({
                  avatarUrlTmpFile: 'http://qnstatic.lianhequnmei.com/' + res.imageURL,
                });
              },
              function (error) {
                // console.log('qiniuUploader,error: ' + error);
              }, {
                region: 'SCN',
                uploadURL: uploadURL,
                // domain: util.getQiNuiBucket,
                uptoken: token,
                // uptokenURL: 'UpTokenURL.com/uptoken',
                qiniuShouldUseQiniuFileName: false,
                key: key,
              });
          }
        })

        _this.setData({
          avatarUrl: res.tempFilePaths[0],
          //   avatarUrlTmpFile: res.tempFilePaths[0]
        })

      }
    })
  },
  // bindPickerChange: function(e) {
  //   this.setData({
  //     genderIndex: e.detail.value,
  //     gender: this.data.genderArray[e.detail.value]
  //   })
  // },
})